package com.barecodes;

/**
 *
 * @author Sawior
 */
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.PdfWriter;
import com.webservice.DaoSingleObject;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.barbecue.BarcodeException;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;
import net.sourceforge.barbecue.output.OutputException;

public class Ean18Generator {
  
    public static File createPdf(String filename, Integer id, ClientsModel client) throws IOException, DocumentException {
        Document document = new Document();
        File file = File.createTempFile("dossier_" + 1 + "_", ".pdf");
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file.getAbsolutePath()));
        document.open();
        Image img;
        BarcodeEAN codeEAN = new BarcodeEAN();
        codeEAN.setCodeType(Barcode.EAN13);
        codeEAN.setSize(300);
        img = com.itextpdf.text.Image.getInstance(saveEanFile(id), null);
        document.add(new Paragraph(String.format("Przepustka")));
        DaoSingleObject dao = new DaoSingleObject();
        ParkingpassesModel pass = dao.getDao().getParkingPassById(id);
        String text = client.getLastName()+" "+client.getName();
        document.add(new Paragraph(text));
        String text2 = "Adres: "+client.getZipCode()+" "+client.getCity()+" ul."+client.getStreet()+" "+client.getHouseNumber()+"/"+client.getLocalNumber();
        document.add(new Paragraph(text2));
        String text3 = "Wazna od: "+pass.getIssueDateformatted()+" do "+pass.getExpDateformatted();
        document.add(new Paragraph(text3));
        document.add(new Paragraph("Kod przepustki:"));
        document.add(img);
        Date date = new Date();
        String text4 = "Data wygenerowania: "+date.toString();
        document.add(new Paragraph(text4));
 
        document.close();
        return file;
    }
    
    public static BufferedImage saveEanFile(Integer id) {
        String codedId = PassesIdCoder.codeId(id);
        net.sourceforge.barbecue.Barcode barcode = null;
        try {
            barcode = BarcodeFactory.createEAN13(codedId);
            barcode.setBarHeight(60);
            Font font = new Font("Dialog", Font.PLAIN, 12);
            barcode.setFont(font);
            barcode.setBarWidth(2);
        } catch (BarcodeException ex) {
            Logger.getLogger(Ean18Generator.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            return BarcodeImageHandler.getImage(barcode);
        } catch (OutputException ex) {
            Logger.getLogger(Ean18Generator.class.getName()).log(Level.SEVERE, "[CarPark] cannot convert barcode image to javafx image", ex);
        }
        return null;
    }
}
